<template>
  <div class="manage">
    <el-dialog
      :title="operateType === 'add' ? '新增用户' : '更新用户'"
      :visible.sync="isShow"
    >
      <common-form
        :formLabel="operateFormLabel"
        :form="operateForm"
        ref="form"
      ></common-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="addUser">+ 新增</el-button>
      <CommonForm inline :formLabel="formLabel" :form="searchFrom">
        <el-button type="primary" @click="getList(searchFrom.keyword)"
          >搜索</el-button
        >
      </CommonForm>
    </div>
    <common-table
      :tableData="tableData"
      :tableLabel="tableLabel"
      :config="config"
      @changePage="getList"
      @edit="editUser"
      @delete="delUser"
    ></common-table>
  </div>
</template>

<script>
import CommonForm from '../../components/CommonForm'
import CommonTable from '../../components/CommonTable'
export default {
  components: {
    CommonForm,
    CommonTable,
  },
  data() {
    return {
      tableData: [],
      tableLabel: [
        {
          prop: 'name',
          label: '姓名',
        },
        {
          prop: 'age',
          label: '年龄',
        },
        {
          prop: 'sexLabel',
          label: '性别',
        },
        {
          prop: 'birth',
          label: '出生日期',
          width: 200,
        },
        {
          prop: 'addr',
          label: '地址',
          width: 320,
        },
      ],
      config: {
        // 分页等配置参数
        page: 1,
        total: 30,
        loading: false,
      },
      operateType: 'add',
      isShow: false,
      // 定义表单需要编辑的字段
      operateForm: {
        name: '',
        addr: '',
        age: '',
        birth: '',
        sex: '',
      },
      operateFormLabel: [
        {
          model: 'name',
          label: '姓名',
        },
        {
          model: 'age',
          label: '年龄',
        },
        {
          model: 'sex',
          label: '性别',
          type: 'select',
          opts: [
            {
              label: '男',
              value: 1,
            },
            {
              label: '女',
              value: 0,
            },
          ],
        },
        {
          model: 'birth',
          label: '出生日期',
          type: 'date',
        },
        {
          model: 'addr',
          label: '地址',
        },
      ],
      searchFrom: {
        keyword: '',
      },
      formLabel: [
        {
          model: 'keyword',
          label: '', // 组件描述的内容
        },
      ],
    }
  },
  methods: {
    getList(name = '') {
      console.log(name)
      this.config.loading = true
      // 搜索时，页码需要设置为1，才能正确返回数据，因为数据是从第一页开始返回的
      // name ? (this.config.page = 1) : '';
      name = '';
      this.$http
        .get('/api/user/getUser', {
          params: {
            page: this.config.page,
            name,
          },
        })
        .then((res) => {
          this.tableData = res.data.list.map((item) => {
            // 处理一下sex的中文显示
            item.sexLabel = item.sex === 0 ? '女' : '男'
            return item
          })
          this.config.total = res.data.count
          this.config.loading = false
        })
    },
    addUser() {
      // this.operateType = 'edit'
      // this.isShow = true
      // this.operateForm = row
      // console.log("addUser")
      console.log('addUser')
      this.operateType = 'add'
      this.isShow = true
    },
    editUser(row) {
      // console.log("editUser")
      // console.log(row)
      // 显示编辑框
      this.operateType = 'edit'
      this.isShow = true
      this.operateForm = row
    },
    delUser(row) {
      // console.log("editUser")
      console.log(row)
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          let id = row.id
          this.$http
            .get('/api/user/del', {
              params: {
                id,
              },
            })
            .then((res) => {
              console.log(res.data)
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
              this.getList()
            })
          // this.$message({
          //   type: 'success',
          //   message: '删除成功!',
          // })
          // 提交给后台进行删除
          console.log('提交给后台进行删除')
          // 删除后进行刷新
          this.getList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    confirm() {
      // if (this.operateType === 'edit') {
      //   console.log("edit", this.operateForm)
      // }
      // else {
      //   console.log("add", this.operateForm)
      // }
      if (this.operateType === 'edit') {
        this.$http.post('/api/user/edit', this.operateForm).then((res) => {
          console.log(res.data)
          this.isShow = false
          this.getList()
        })
      } else {
        this.$http.post('/api/user/add', this.operateForm).then((res) => {
          console.log(res.data)
          this.isShow = false
          this.getList()
        })
      }
      // 无论是新增还是编辑都需要重新刷新表格
      this.getList()
    },
  },
  created() {
    this.getList()
  },
}
</script>

<style lang="scss" scoped>
@import '@/assets/scss/common';
</style>
